package com.gxgeek.springbootquartz.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author gaoxing
 * @create 2017-12-11 下午5:08
 */
public class HelloScheduler {

    public static void main(String[] args) throws SchedulerException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Time is:" + simpleDateFormat.format(date));
        //创建一个JobDetail实例， 将该实例与hellojob class 绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob","group1")
                              .usingJobData("name","jobDetail")
                              .usingJobData("floatValue",1.1F)
                              .usingJobData("doubleValue",1000000000000.12)
                              .usingJobData("message","message")
                              .build();

        //创建一个Trigger实例，定义该job立即执行，并且每隔两秒钟重复执行一次
        //1 . 2017 年 每天十点十五分
//        * 15 10 ? * * 2017

//1.     每天的14点整 至 14点59分59秒， 以及 18点整 至18点59分55 秒 每五秒触发
//        */5 * 14,18 ? * * 
        CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger().withIdentity("myTrigger","group1")
                .startAt(date)
                .withSchedule(
//                        CronScheduleBuilder.cronSchedule("* * * * * ? *"))
                        CronScheduleBuilder.cronSchedule("* * * * * ? *"))
//                  .usingJobData("name","trigger")
                  .build();


        //创建Schedule实例
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail,trigger);

    }

}
